package com.wjl.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.wjl.guli.common.base.R;
import com.wjl.guli.service.edu.entity.Teacher;
import com.wjl.guli.service.edu.entity.vo.TeacherVo;
import com.wjl.guli.service.edu.feign.OssFileService;
import com.wjl.guli.service.edu.mapper.TeacherMapper;
import com.wjl.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.TabableView;
import java.sql.Wrapper;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author wjl
 * @since 2020-11-24
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    OssFileService ossFileService;

    @Override
    public IPage pageQuery(Page page, TeacherVo vo) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByAsc("sort");
        if (vo==null){
            return baseMapper.selectPage(page,queryWrapper);
        }
        String name = vo.getName();
        String level = vo.getLevel();
        String firstJoinDate = vo.getFirstJoinDate();
        String lastJoinDate = vo.getLastJoinDate();
        if (!StringUtils.isNullOrEmpty(name)){
            queryWrapper.likeRight("name",name);
        }
        if (!StringUtils.isNullOrEmpty(level)){
            queryWrapper.eq("level",level);
        }
        if (!StringUtils.isNullOrEmpty(firstJoinDate)){
            queryWrapper.ge("join_date",firstJoinDate);
        }
        if (!StringUtils.isNullOrEmpty(lastJoinDate)){
            queryWrapper.le("join_date",lastJoinDate);
        }
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.select("name");
        queryWrapper.likeRight("name",key);
        List<Map<String, Object>> mapList= baseMapper.selectMaps(queryWrapper);

        return mapList;
    }

    @Override
    public boolean removeAvatarById(Long id) {

        Teacher teacher = baseMapper.selectById(id);
        if (teacher!=null){
            String avatarUrl = teacher.getAvatar();
            if (!StringUtils.isNullOrEmpty(avatarUrl)){
                R r = ossFileService.removeAvatar(avatarUrl);
                return r.getSuccess();
            }
        }



        return false;
    }
}
